package com.shop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

public class Step6 extends Configured implements Tool {
    static class Step6Mapper extends Mapper<Text, Text, Text, Text> {
        @Override
        protected void map(Text key, Text value, Mapper<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
            context.write(key, value);
        }
    }

    static class Step6Reducer extends Reducer<Text, Text, Text, DoubleWritable> {
        @Override
        protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, DoubleWritable>.Context context) throws IOException, InterruptedException {
            HashMap<String, Double> userMap = new HashMap<>();
            HashMap<String, Double> shopMap = new HashMap<>();
            for (Text value : values) {
                String[] datas = value.toString().split(",");
                for (String data : datas) {
                    if (data.contains("-")) {
                        String[] shops = data.split("-");
                        shopMap.put(shops[0], Double.valueOf(shops[1]));
                    } else if (data.contains(":")) {
                        String[] users = data.split(":");
                        userMap.put(users[0], Double.valueOf(users[1]));
                    }
                }
            }
            for (Map.Entry<String, Double> user : userMap.entrySet()) {
                for (Map.Entry<String, Double> shop : shopMap.entrySet()) {
                    String k3 = user.getKey() + "," + shop.getKey();
                    Double v3 = user.getValue() * shop.getValue();
                    context.write(new Text(k3),new DoubleWritable(v3));
                }
            }
        }
    }

    @Override
    public int run(String[] strings) throws Exception {
        Configuration conf = getConf();
        // 指定输入输出路径
        Path step6input1 = new Path(
                "hdfs://192.168.10.11:9000/shop2output");
        Path step6input2 = new Path(
                "hdfs://192.168.10.11:9000/shop5output");
        Path step6output = new Path(
                "hdfs://192.168.10.11:9000/shop6output");

        FileSystem fs = FileSystem.get(
                new URI("hdfs://192.168.10.11:9000")
                , conf);
        if (fs.exists(step6output)) fs.delete(step6output, true);
        //构建Job
        Job job = Job.getInstance(conf);
        job.setJobName("step6");
        job.setJarByClass(this.getClass());

        job.setMapperClass(Step6Mapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setReducerClass(Step6Reducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(DoubleWritable.class);

        job.setInputFormatClass(SequenceFileInputFormat.class);
        SequenceFileInputFormat.addInputPath(job, step6input1);
        SequenceFileInputFormat.addInputPath(job, step6input2);

//        job.setOutputFormatClass(TextOutputFormat.class);
//        TextOutputFormat.setOutputPath(job, step6output);
        job.setOutputFormatClass(SequenceFileOutputFormat.class);
        SequenceFileOutputFormat.setOutputPath(job, step6output);
        return job.waitForCompletion(true) ? 0 : -1;
    }

    public static void main(String[] args) throws Exception {
        System.exit(ToolRunner.run(new Step6(),args));
    }
}
